#include "RotationImageSettingsForm.h"
#include "ui_RotationImageSettingsForm.h"
#include <QDebug>

RotationImageSettingsForm::RotationImageSettingsForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RotationImageSettingsForm),
    m_rotationAngle(0)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_StyledBackground, true);
    // 初始化UI - 设置默认角度为0
    ui->rotationAngleSlider->setValue(0);
    ui->rotationAngleSpinBox->setValue(0);
}

RotationImageSettingsForm::~RotationImageSettingsForm()
{
    delete ui;
}

void RotationImageSettingsForm::setRotationAngle(int angle)
{
    m_rotationAngle = angle;
    
    // 更新UI控件，断开信号避免循环触发
    disconnect(ui->rotationAngleSlider, &QSlider::valueChanged, this, nullptr);
    disconnect(ui->rotationAngleSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, nullptr);
    
    ui->rotationAngleSlider->setValue(angle);
    ui->rotationAngleSpinBox->setValue(angle);
    
    // 重新连接信号
    connect(ui->rotationAngleSlider, &QSlider::valueChanged, this, &RotationImageSettingsForm::on_rotationAngleSlider_valueChanged);
    connect(ui->rotationAngleSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &RotationImageSettingsForm::on_rotationAngleSpinBox_valueChanged);
}

int RotationImageSettingsForm::rotationAngle() const
{
    return m_rotationAngle;
}

void RotationImageSettingsForm::on_rotationAngleSlider_valueChanged(int value)
{
    int oldAngle = m_rotationAngle;
    m_rotationAngle = value;
    
    // 更新spinBox，断开信号避免循环触发
    disconnect(ui->rotationAngleSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, nullptr);
    ui->rotationAngleSpinBox->setValue(value);
    connect(ui->rotationAngleSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &RotationImageSettingsForm::on_rotationAngleSpinBox_valueChanged);
    
    // 如果设置发生变化，发出信号
    if (oldAngle != m_rotationAngle) {
        emit settingsChanged();
    }
}

void RotationImageSettingsForm::on_rotationAngleSpinBox_valueChanged(int arg1)
{
    int oldAngle = m_rotationAngle;
    m_rotationAngle = arg1;
    
    // 更新slider，断开信号避免循环触发
    disconnect(ui->rotationAngleSlider, &QSlider::valueChanged, this, nullptr);
    ui->rotationAngleSlider->setValue(arg1);
    connect(ui->rotationAngleSlider, &QSlider::valueChanged, this, &RotationImageSettingsForm::on_rotationAngleSlider_valueChanged);
    
    // 如果设置发生变化，发出信号
    if (oldAngle != m_rotationAngle) {
        emit settingsChanged();
    }
}